<template>
	<view>
		<view class="dogner-form-item">
			<view class="donger-from-item-label">车牌号：</view>
			<view class="donger-from-item-obj"> <input class="uni-input" :focus="editable" placeholder="请输入车牌号" :disabled="!editable"
				 v-model="car.number" /></view>
		</view>
		<view class="dogner-form-item">
			<view class="donger-from-item-label">是否自卸：</view>
			<view class="donger-from-item-obj">
				<picker :value="index" :range="autoDropOptions">
					<view class="uni-input">{{autoDropOptions[index]}}</view>
				</picker>
			</view>
		</view>
		<view class="dogner-form-item">
			<view class="donger-from-item-label">排放标准：</view>
			<view class="donger-from-item-obj">
				<picker :value="index" :range="oilStandardOptions">
					<view class="uni-input">{{oilStandardOptions[index]}}</view>
				</picker>
			</view>
		</view>
		<view class="dogner-form-item">
			<view class="donger-from-item-label">轴数：</view>
			<view class="donger-from-item-obj"> <input class="uni-input" type="number" :disabled="!editable" placeholder="请输入轴数"
				 v-model="car.axis" /></view>
		</view>
		<view class="dogner-form-item">
			<view class="donger-from-item-label">品牌：</view>
			<view class="donger-from-item-obj"> <input class="uni-input" :disabled="!editable" placeholder="请输入品牌" v-model="car.brand" /></view>
		</view>

		<view class="dogner-form-item" style="border-bottom-width: 0;" v-if="editable">
			<button style="width: 100%;" type="primary" @click="save()">保存</button>
		</view>

	</view>
</template>

<script>
	import tradeService from '@/api/trade.js';
	export default {
		data() {
			return {
				editable: true,
				index: 0,
				autoDropOptions: ["是", "否"],
				oilStandardOptions: ["国5", "国4"],
				autoDropOptions: ["是", "否"],
				autoDropOptions: ["是", "否"],
				car: {
					id: ""
				}
			};
		},
		methods: {
			getDetail: function(id) {
				tradeService.carDetail(id).then((res) => {
					console.log(JSON.stringify(res.data.data));
					if (res.data.msg) {
						uni.showToast({
							icon: "none",
							title: res.data.msg,
							duration: 2000
						});
					}

					if (res.data.success) {
						this.car = res.data.data;
					}
				});
			},
			save: function() {
				uni.showLoading({
					title: '保存中'
				});
				console.log(JSON.stringify(this.car));
				tradeService.carSave(this.car).then((res) => {
					uni.hideLoading();
					console.log(JSON.stringify(res));

					if (res.data.success) {
						if (res.data.msg) {
							uni.showToast({
								icon: "success",
								title: res.data.msg,
								duration: 2000
							});
						}
						uni.navigateTo({
							url: "/pages/cars/list/list"
						});
					} else {
						if (res.data.msg) {
							uni.showToast({
								icon: "none",
								title: res.data.msg,
								duration: 2000
							});
						}
					}
				});
			}
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			if (option.action === "detail") {
				this.editable = false;
				this.getDetail(option.id);
			} else if (option.action === "add") {
				this.editable = true;
				this.car.id = "";
			} else if (option.action === "edit") {
				this.editable = true;
				this.getDetail(option.id);
			}
		}
	}
</script>

<style>

</style>
